/**
 *Author: Andrew Welty
 *Team: Gradusoft (Team 2) 
 */
package virtualadvisor;

import java.util.ArrayList;


public class Semester implements Comparable<Semester> {
	
	//Variables
	private String season = "";
	private int year;
	private int totalCredits;
	private ArrayList<Course> courses;
	private static final int MAX_CREDITS = 20;
	
	//Semester Constructor
	public Semester(String season, int year) {
		this.season = season;
		this.year = year;
		this.courses = new ArrayList<Course>();
	}
	
	/**
	* Name: addCourse()
	* PreCondition:  totalCredits + newCredits must be less than MaxCredits
	* PostCondition: Course is added to the semester and totalCredits is incremented
	* @param newCourse
	* @return void
	*/
	public void addCourse(Course newCourse) {
		if(totalCredits + newCourse.getCredits() < (MAX_CREDITS)
				&& checkDuplicate(newCourse) == false) 
		{
			courses.add(newCourse);
			totalCredits += newCourse.getCredits();
		}
		else{
			//JOptionPane.showMessageDialog(contentPanel, "Can't exceed more than 20 credits","Error", JOptionPane.ERROR_MESSAGE);
			System.out.println("Unable to add course.");
		}
	}
	
	/**
	* Name: deleteCourse()
	* PreCondition:  Course must exist in the list in order to be deleted
	* PostCondition: Course is deleted from the semester and totalCredits is decremented
	* @param course
	* @return void
	*/
	public void deleteCourse(Course course) {
		for(int i = 0; i < courses.size(); i++) 
		{
			if(courses.get(i).equals(course))
			{
				courses.remove(i);
			}
			else{
				System.out.println("That course does not exist");
			}
		}
	}
	
	public void deleteCourse(String course) {
		for(int i = 0; i < courses.size(); i++) {
			if(courses.get(i).getName().equals(course)) {
				courses.remove(i);
				return;
			} else
				System.out.println("That course does not exist");
		}
	}
	
	/**
	* Name: checkAppropriateCreds()
	* PreCondition:  none
	* PostCondition: none
	* @param none
	* @return boolean
	*/
	public boolean checkAppropriateCreds() {
		if(totalCredits <= 20)
		{
			return true;
		}
			
		return false;
	}
	
	/**
	 * Name: equals()
	 * Precondition: must be existing course to compare to
	 * Postcondition: returns true if two semesters are equivalent, false otherwise
	 * @param newCourse
	 * @return boolean
	 */
	
	public boolean equals(Object sem)
	{
		Semester semester = (Semester)sem;
		int numSame = 0;
		for (int i = 0; i < courses.size(); i++)
		{
			if(semester.courses.get(i).equals(this.courses.get(i)))
			{
				numSame++;
			}
		}
		if(numSame == courses.size() 
				&& this.season == semester.getSeason()
				&& this.year == semester.getYear())
		{
			return true;
		}
		return false;
	}
	
	
	/**
	 * Name: compareTo()
	 * Description: 
	 * PreCondition:  none
	 * PostCondition: none
	 */
	public int compareTo(Semester arg0) {
		int ret = 0;
		if(this.year == arg0.year) {
			String ourSeason = this.season.toLowerCase();
			String theirSeason = arg0.season.toLowerCase();

			//ret = 0, our semester is their semester.
			//ret = -1, our semester comes before their semester.
			//ret = 1, our semester comes after their semester.
			if(ourSeason.equals("summer") && theirSeason.equals("summer"))     {ret =  0;}
			else if(ourSeason.equals("summer") && theirSeason.equals("fall"))  {ret =  1;}
			else if(ourSeason.equals("summer") && theirSeason.equals("winter")){ret =  1;}
			else if(ourSeason.equals("summer") && theirSeason.equals("spring")){ret =  1;}
			else if(ourSeason.equals("fall") && theirSeason.equals("summer"))  {ret = -1;}
			else if(ourSeason.equals("fall") && theirSeason.equals("fall"))    {ret =  0;}
			else if(ourSeason.equals("fall") && theirSeason.equals("winter"))  {ret = -1;}
			else if(ourSeason.equals("fall") && theirSeason.equals("spring"))  {ret = -1;}
			else if(ourSeason.equals("winter") && theirSeason.equals("summer")){ret = -1;}
			else if(ourSeason.equals("winter") && theirSeason.equals("fall"))  {ret =  1;}
			else if(ourSeason.equals("winter") && theirSeason.equals("winter")){ret =  0;}
			else if(ourSeason.equals("winter") && theirSeason.equals("spring")){ret = -1;}
			else if(ourSeason.equals("spring") && theirSeason.equals("summer")){ret = -1;}
			else if(ourSeason.equals("spring") && theirSeason.equals("fall"))  {ret =  1;}
			else if(ourSeason.equals("spring") && theirSeason.equals("winter")){ret =  1;}
			else if(ourSeason.equals("spring") && theirSeason.equals("spring")){ret =  0;}
		}
		else if(this.year < arg0.year){ret = -1;}
		else if(this.year > arg0.year){ret =  1;}
		return ret;
	}
	
	public boolean contains(Course c){
		if(checkDuplicate(c))
			return true;
		
		return false;
	}
	/**
	 * Name: checkDuplicate()
	 * PreCondition:  none
	 * PostCondition: none
	 * @param newCourse
	 * @return boolean
	 */
	private boolean checkDuplicate(Course newCourse) {
		for (int i = 0; i < courses.size(); i++)
		{
			if(courses.get(i).equals(newCourse))
			{
				return true; //if course already exists then there is truly a duplicate
			}
		}
		return false;
	}
	
	/**
	 * Name: compareCourses() 
	 * PreCondition:  none
	 * PostCondition: none
	 * @param newCourse
	 * @return boolean
	 */
	/*
	private boolean compareCourses(Course newCourse) {
		//Check to see if you don't already have the course added
		
		return true;
	}
	*/
	
	
	/**
	 * Name: getSeason()
	 * PreCondition: none
	 * PostCondition: returns the season as a string
	 * @param none
	 * @return string season
	 */
	public String getSeason() { return this.season; }
	
	
	/**
	 * Name: getYear
	 * PreCondition: none
	 * PostCondition: returns the year of the semester as an integer
	 * @param none
	 * @return integer year
	 */
	public int getYear() { return this.year; }
	
	
	/**
	 * Name: getSeasonYear
	 * Precondition: None
	 * Postcondition: Gets the season and year as a string
	 * @return - Season and year as string
	 */
	public String getSeasonYear(){
		return season + year;
	}
	

	/**
	 * Name: getTotalCredits()
	 * Precondition: None
	 * Postcondition: Gets the season and year as a string
	 * @return - Season and year as string
	 */
	public int getTotalCredits() {
		return totalCredits;
	}

	/**
	 * Name: getTotalCredits()
	 * Precondition: None
	 * Postcondition: Gets the season and year as a string
	 * @return - Season and year as string
	 */
	public ArrayList<Course> getCourses() {
		return courses;
	}
	
	/**
	 * Name: XMLCoder
	 * Precondition: None
	 * Postcondition: Gathers information on class to be displayed 
	 *              in XML format for saving to file.
	 * @return - Output on the class to be displayed in XML format
	 */
	public String XMLCoder() {
		String xml = "";
		
		for(int x = 0; x < courses.size(); x++) 
			xml = xml + courses.get(x).XMLCoder();
	      
		return xml;
	}
	
	public String toString() {
		String text = "";
		
		text = "Semester: " + getSeason() + " " + getYear() + "\n\n";
		text = text + "Course Number     " + "Course Description                                   " + "Credits\n";
		text = text + "------------------------------------------------------------------------------------------\n";
		for(int x = 0; x < courses.size(); x++)
			text = text + scheduleToString(courses.get(x))+ "\n";
		text = text + "------------------------------------------------------------------------------------------\n";
		text = text + "                                                                    Total: " + getTotalCredits() + "\n\n";
		
		return text;
	}
	
	/**
	 * Name: scheduleToString
	 * Precondition: None
	 * Postcondition: Gathers information on the schedule to be displayed
	 * @param course - Course to get schedule information.
	 * @return - Output on the class to be displayed
	 */
	private String scheduleToString(Course course){
		String text = "";
		
		text = course.getDept() + course.getNumber();
		text = text + "           " + course.getTitle();
		text = text + "                            " + course.getCredits();
		
		return text;
	}
}